package com.copm.ifm.base.service.util;

import cn.hutool.core.util.ObjectUtil;
import com.copm.ifm.base.basic.exception.ServiceException;
import com.copm.ifm.base.basic.pojo.BaseTreePO;
import com.copm.ifm.base.basic.pojo.vo.TreeVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;

/**
 * 树状结构工具类
 *
 * @author zzf
 * @date 2020/7/30 11:14
 */
public class TreeUtil {

    public static final int DEFAULT_PATH_LENGTH = 4;

    private static final DecimalFormat format = new DecimalFormat("0000");

    public static final String FIRST_NODE = "0001";

    /**
     * 递归生成树状结构
     */
    public static TreeVO getTree(Collection<? extends BaseTreePO> list) {
        TreeVO treeVO = TreeVO.head();
        TreeUtil.getTree(list, treeVO);
        return treeVO;
    }

    /**
     * 递归生成树状结构
     */
    private static void getTree(Collection<? extends BaseTreePO> list, TreeVO parentNode) {
        List<TreeVO> childList = new ArrayList<>();
        list.forEach(s -> {
            if (isChild(s, parentNode)) {
                TreeVO treeVO = doToDTO(s);
                if (!s.getLeafNode()) {
                    getTree(list, treeVO);
                }
                childList.add(treeVO);
            }
        });
        //排序
        if (ObjectUtil.isNotEmpty(childList)) {
            childList.sort(Comparator.comparingInt(TreeVO::getOrderNum));
        }
        parentNode.setChildList(childList);
    }

    /**
     * 对象转换
     */
    private static TreeVO doToDTO(BaseTreePO treeDO) {
        TreeVO treeVO = new TreeVO();
        BeanUtils.copyProperties(treeDO, treeVO);
        return treeVO;
    }

    /**
     * 判断是否为其子节点
     *
     * @param treeDO     树状实体对象
     * @param parentNode 父级节点对象
     */
    private static boolean isChild(BaseTreePO treeDO, TreeVO parentNode) {
        if (StringUtils.isEmpty(parentNode.getPath())) {
            return StringUtils.isEmpty(treeDO.getParentPath());
        } else {
            return parentNode.getPath().equals(treeDO.getParentPath());
        }
    }

    /**
     * 获得第一个子集的编号
     */
    public static String getFirstChildPath(String parentPath) {
        return (parentPath == null ? "" : parentPath) + format.format(1);
    }

    /**
     * 获得下一个子集的编号
     *
     * @param prevChildPath 上一个子集的编号
     */
    public static String getNextChildPath(String prevChildPath) {
        Number num;
        try {
            num = format.parse(prevChildPath.substring(prevChildPath.length() - DEFAULT_PATH_LENGTH));
        } catch (ParseException e) {
            throw new ServiceException("编码生成错误，请联系管理员~");
        }
        return getParentPath(prevChildPath) + format.format(num.intValue() + 1);
    }

    /**
     * 获得父路径
     */
    public static String getParentPath(String childPath) {
        int level = childPath.length() / DEFAULT_PATH_LENGTH;
        return childPath.substring(0, (level - 1) * DEFAULT_PATH_LENGTH);
    }

    /**
     * 根据路径获得层级
     */
    public static Integer getLevel(String path) {
        if (path == null) {
            return 0;
        }
        return path.length() / DEFAULT_PATH_LENGTH;
    }

    /**
     * 获取路径当层的序号
     */
    public static String getPathEnd(String path) {
        return path.substring(path.length() - DEFAULT_PATH_LENGTH);
    }

}
